<?php

	namespace Admin\Controller;

	class FriendlinkController extends AdminController {


		// 显示所有的友情链接
		public function link()
		{
			// $link = D('friendlink');
			// $data = $link->linkshow();
			
			// 获取查询条件
			$linkname       =   I('linkname');
      $map['status']  =   array('egt',0);
      if(is_numeric($linkname)){
          $map['id|linkname|company']=   array(intval($linkname),array('like','%'.$linkname.'%'),'_multi'=>true);
      }else{
          $map['linkname|company']    =   array('like', '%'.(string)$linkname.'%');
      }

			$data = $this->lists('friendlink',$map);
			$status = ['禁用','启用'];
      foreach($data as &$val){
      	$val['status'] = $status[$val['status']];
      }
			$this->assign('data' , $data);
			$this->display();
		}

		// 添加
		public function increase()
		{
			// 显示页面
			if (IS_GET) {
				$link = D('friendlink');
				$data = $link->linkshow();
				$this->assign('data' , $data);
				$this->display();
			}

			// 添加的action
			if (IS_POST) {
				$link = D('friendlink');
				$res = $link->increaseshow();
				if ($res) {
					$this->success('友情链接添加成功' , U('link'));
				}else{
					$this->success('添加失败' , U('increase'));
					$this->display();
				}
				
			}
		}
		//修改友情链接
		public function modify()
		{
			if (IS_GET) {
				$id = I('get.id');
				$link = D('friendlink');
				$map['id'] = ['eq' , $id];
				$list = $link->where($map)->select();
				$this->assign('data' , $list);
				$this->assign('id' , $id);
				$this->display();
			}

			// 添加的action
			if (IS_POST) {
				$link = D('friendlink');
				$res = $link->modify();

				if ($res == 0 || $res == 1) {
					$this->success('友情链接添加成功' , U('link'));
				}else{
					$this->error('添加失败' , U('modify',array()));
					$this->display();
				}
				
			}
			
		}

		// 友情链接修改状态
		public function status(){
			$link = D('friendlink');
			$res = $link->status();
			$this->ajaxReturn($res);
		}
	}
